package com.gmrz.uaf.servlet.v1.handler;

import com.gmrz.uaf.common.Constants;
import com.gmrz.uaf.common.GuiceUtil;
import com.gmrz.uaf.protocol.v1.json.UAFSchemaBuilder;
import com.gmrz.uaf.protocol.v1.processor.UAFAddUviProcessor;
import com.gmrz.uaf.protocol.v1.schema.AdditionUvi;
import com.gmrz.uaf.server.ServerConfig;
import com.gmrz.uaf.server.ServerContext;
import com.gmrz.uaf.servlet.v1.request.AddUviContext;
import com.gmrz.uaf.servlet.v1.request.ReceiveReqPayload;
import com.gmrz.uaf.servlet.v1.response.FidoRespPayload;
import com.gmrz.uaf.servlet.v1.validation.PayloadValidationException;
import com.gmrz.uaf.servlet.v1.validation.RestValidator;
import com.gmrz.util.Convert;
import com.gmrz.util.CryUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Type;
import java.util.List;

/**
 *
 */
public class UAFAddUviHandler extends UAFBaseHandler {

    private static final Logger LOG = LogManager.getLogger(UAFAddUviHandler.class);


    /**新增指纹
     * @param request
     */
    public String addUvi(String request) {

        LOG.info("[/req/adduvi][begin]");
        ServerContext.getServerContext().getFidoLog().setRequest(request);
        FidoRespPayload<List<String>> respPayload = new FidoRespPayload<List<String>>();
        String response = null;
        try {


            Gson gson = UAFSchemaBuilder.getGson();
            Type reqType = new TypeToken<ReceiveReqPayload<AddUviContext>>() {
            }.getType();

            ReceiveReqPayload<AddUviContext> reqPayload = gson.fromJson(request, reqType);
            ServerConfig serverConfig = GuiceUtil.getProcessorInjector().getInstance(ServerConfig.class);
            String json = new String(CryUtil.decryptAES128(Convert.fromBase64(reqPayload.getContext().getAdditionuvi()), serverConfig.getEncryptionKey()));
            AdditionUvi additionUvi = gson.fromJson(json, AdditionUvi.class);
            LOG.debug("additionUvi json:" + json);
            logRequest(request, reqPayload.getContext().getTransNo(), additionUvi.getUserName(),
                    additionUvi.getAppID(), null,null, Constants.Method.ADD_UVI);
            validate(reqPayload);
            UAFAddUviProcessor processor = GuiceUtil.getProcessorInjector().
                    getInstance(UAFAddUviProcessor.class);

            processor.addUvi(additionUvi);
            respPayload.setStatusCode(1200);
            respPayload.setDescription("OK");
            return gson.toJson(respPayload);
        } catch (Exception ex) {
            LOG.error(ex);
            response = buildFidoRespFromException(respPayload, ex);
        }
        LOG.info("[/device/support][end]");
        return response;
    }

    private void validate(ReceiveReqPayload<AddUviContext> reqPayload)
            throws PayloadValidationException {
        if ((reqPayload == null) || (reqPayload.getContext() == null)) {
            throw new PayloadValidationException("context", RestValidator.VALIDATION_ERROR.EMPTY);
        }
        RestValidator.validate("transNo", reqPayload.getContext().getTransNo());
        RestValidator.validate("additionuvi", reqPayload.getContext().getAdditionuvi());
    }
}


